import Vue from 'vue'
import VueRouter from 'vue-router'
import { routeHistoryManager } from '@/utils/routeHistoryManager';
Vue.use(VueRouter)

/* Layout */
import Layout from '@/layout'


const routes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect')
      }
    ]
  },
  {
    path: '',
    component: Layout,
    redirect: 'index',
    children: [
      {
        path: '',
        component: () => import('@/views/agentHome/index'),
        name: 'index',
        meta: { title: '核桃编程', icon: 'dashboard', affix: true },
        beforeEnter: (to, from, next) => {
          next('/WorkflowManagement');
        }
      },
      {
        path: 'WorkflowManagement',
        component: () => import('@/views/WorkflowManagement/index'),
        name: 'WorkflowManagement',
        meta: { title: '工作流管理', icon: 'dashboard', affix: true }
      },
      {
        path: 'ResourceAllocation',
        component: () => import('@/views/ResourceAllocation/index'),
        name: 'ResourceAllocation',
        meta: { title: '独享资源配置管理', icon: 'user', affix: true }
      },
      {
        path: '',
        component: { render: h => h('router-view') }, // 关键修改：确保能渲染子路由
        meta: { title: '监控管理', icon: 'user' },
        children: [
          {
            path: '', // 空路径，作为默认显示
            component: { render: h => h('div') }, // 空组件
            meta: { hidden: true } // 隐藏这个空路由
          },
          {
            path: 'MonitoringReport',
            component: () => import('@/views/MonitoringReport/index'),
            name: 'MonitoringReport',
            meta: { title: '监控报表', icon: 'dashboard' }
          },
          {
            path: 'MonitoringReporttoken',
            component: () => import('@/views/MonitoringReport/token'),
            name: 'MonitoringReporttoken',
            meta: { title: 'Token监控', icon: 'dashboard' }
          }
        ]
      }
    ]
  },


]



// 获取原型对象push函数
const VueRouterPush = VueRouter.prototype.push
// 修改原型对象中的push函数
VueRouter.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch(err => err)
}


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  document.title = process.env.VUE_APP_TITLE
  routeHistoryManager.addRoute(to);
  next();
});


export default router